package com.alibaba.system.service.impl;

import com.alibaba.system.domain.Role;
import com.alibaba.system.mapper.RoleMapper;
import com.alibaba.system.service.IRoleService;
import com.alibaba.basic.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 业务实现类：
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {

    @Resource
    private RoleMapper roleMapper;

    @Override
    public void add(Role role){
        //添加t_role 表信息
        roleMapper.save(role);
        //添加数据
        insertRelationTable(role);
    }

    @Override
    public void del(Long id){
        Role role = roleMapper.loadById(id);
        initRelationTable(role);
        roleMapper.remove(id);
    }

    @Override
    public void update(Role role){
        initRelationTable(role);
        roleMapper.update(role);
        insertRelationTable(role);
    }

    //初始化 role关系表
    public void initRelationTable(Role role){
        roleMapper.removeRoleMenu(role.getId());
        roleMapper.removeRolepermission(role.getId());
    }

    //添加 role 关联表数据
    public void insertRelationTable(Role role){
        roleMapper.addRoleMenu(role.getId(),role.getMenus());
        roleMapper.addRolepermission(role.getId(),role.getPermissions());
    }

}
